<?php
/*          Document   : JO_voteController
            Created on : Aug 23, 2011, 10:43:03 PM
            Author     : Khuong Truong
 *
 *          To change this template, choose Tools | Templates
 *          and open the template in the editor.
 */
if(isset($_REQUEST["JO_action"])){
    $action=$_REQUEST["JO_action"];
    
    switch ($action){
        
        case "JO_addvote":
            if(isset($_SESSION[ "JO_account_id" ])){
                
                $n=voteModule::insertVote($_REQUEST["JO_vote_key"],$_SESSION[ "JO_account_id" ],"null");
                if($n){
                    echo "1";
                }else{
                    echo "0";
                }
            }else{
                echo "-1";
            }
            break;
        case "JO_reloadvotenum":
            echo voteModule::getVotenum($_REQUEST["JO_vote_key"]);
            break;

    }
}

class voteModule {

    public static  function getVoteList_Vote($vote_key) {
        $SQLCommand="select * from vote_list_artist where vote_key=".$vote_key;
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data->num_rows) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
    public static  function getVotes() {
        $SQLCommand="select * from vote";
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data->num_rows) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
    public static  function getVotenum($_vote_list_key) {
        $SQLCommand="select count(*) as vote_num  from vote_list,vote_process where vote_list.`key`=vote_process.vote_list and `account` is not null and vote_list.`key`=".$_vote_list_key;
        $data=DataProvider::executeSQL($SQLCommand);

        if($data->num_rows) while ($row=$data->fetch_array()){
                return isset($row["vote_num"])?$row["vote_num"]:0;
        }else        return 0;
        
    }
    public static  function getVote($_vote_key) {
        $SQLCommand="select vote.*,(DAYOFYEAR(now())-DAYOFYEAR(if(isnull(vote.refresh_date),vote.start_date,vote.refresh_date))/vote.vote_refresh) as _refresh from vote where `key`=".$_vote_key;
        $data=DataProvider::executeSQL($SQLCommand);
        $row=array();
        if($data!=null)
        {
            $row=$data->fetch_array();
            
        }
        else        return null;
        return $row;
    }
    public static  function insertVote($_vote_key,$_account,$_phone) {
        $SQLCommand="call I_vote_process(".$_vote_key.",".$_account.",".$_phone.")";
        $n=DataProvider::executenoneSQL($SQLCommand);
        
        if($n==1)
                return true;
        return false;
    }
    public static  function RefreshVote($_vote_key) {
        $SQLCommand="call U_refresh_vote_list(".$_vote_key.")";
        $n=DataProvider::executenoneSQL($SQLCommand);

        if($n==1)
                return true;
        return false;
    }

}
?>